package com.smart.campus.web.controller.attendance;

import java.util.List;

import com.smart.campus.school.domain.AttendanceGroupUser;
import com.smart.campus.school.domain.request.AttendanceGroupUserRequest;
import com.smart.campus.school.service.IAttendanceGroupUserService;
import com.smart.campus.web.utils.SchoolUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.AttendanceGroup;
import com.smart.campus.school.service.IAttendanceGroupService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;

/**
 * 考勤组Controller
 * 
 * @author Zhihe.Li
 * @date 2020-10-21
 */
@Controller
@RequestMapping("/school/attendance/group")
public class AttendanceGroupController extends BaseController
{
    private String prefix = "school/attendance/group";

    @Autowired
    private IAttendanceGroupService attendanceGroupService;

    @Autowired
    private IAttendanceGroupUserService attendanceGroupUserService;

    @RequiresPermissions("school:group:view")
    @GetMapping()
    public String group()
    {
        return prefix + "/group";
    }

    /**
     * 查询考勤组列表
     */
    @RequiresPermissions("school:group:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AttendanceGroup attendanceGroup)
    {
        startPage();
        attendanceGroup.setSchoolId(SchoolUtil.getSchoolId());
        List<AttendanceGroup> list = attendanceGroupService.selectAttendanceGroupList(attendanceGroup);
        return getDataTable(list);
    }

    /**
     * 导出考勤组列表
     */
    @RequiresPermissions("school:group:export")
    @Log(title = "考勤组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AttendanceGroup attendanceGroup)
    {
        attendanceGroup.setSchoolId(SchoolUtil.getSchoolId());
        List<AttendanceGroup> list = attendanceGroupService.selectAttendanceGroupList(attendanceGroup);
        ExcelUtil<AttendanceGroup> util = new ExcelUtil<AttendanceGroup>(AttendanceGroup.class);
        return util.exportExcel(list, "group");
    }

    /**
     * 新增考勤组
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存考勤组
     */
    @RequiresPermissions("school:group:add")
    @Log(title = "考勤组", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AttendanceGroup attendanceGroup)
    {
        attendanceGroup.setSchoolId(SchoolUtil.getSchoolId());
        attendanceGroup.setStatus(1);
        return toAjax(attendanceGroupService.insertAttendanceGroup(attendanceGroup));
    }

    /**
     * 修改考勤组
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AttendanceGroup attendanceGroup = attendanceGroupService.selectAttendanceGroupById(id);
        mmap.put("attendanceGroup", attendanceGroup);
        return prefix + "/edit";
    }

    /**
     * 修改保存考勤组
     */
    @RequiresPermissions("school:group:edit")
    @Log(title = "考勤组", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AttendanceGroup attendanceGroup)
    {
        return toAjax(attendanceGroupService.updateAttendanceGroup(attendanceGroup));
    }

    /**
     * 删除考勤组
     */
    @RequiresPermissions("school:group:remove")
    @Log(title = "考勤组", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(attendanceGroupService.deleteAttendanceGroupByIds(ids));
    }



    @RequiresPermissions("school:group:view")
    @GetMapping("groupUser")
    public String groupUser(Long groupId, ModelMap mmap)
    {
        mmap.put("groupId", groupId);
        return prefix + "/groupUser";
    }
    /**
     * 获取分组人员列表
     */
    @RequiresPermissions("school:group:list")
    @PostMapping("/getGroupUserList")
    @ResponseBody
    public TableDataInfo getGroupUserList(AttendanceGroupUser attendanceGroup)
    {
        startPage();
        return getDataTable(attendanceGroupUserService.selectAttendanceGroupUserList(attendanceGroup));
    }

    /**
     * 新增保存考勤组
     */
    @RequiresPermissions("school:group:add")
    @PostMapping("/addUser")
    @ResponseBody
    public AjaxResult addUser(AttendanceGroupUserRequest request)
    {
        String[] ids = request.getUserIds().split(",");
        String[] userNames = request.getUserNames().split(",");
        int index = 0;
        for (String id : ids) {
            AttendanceGroupUser user = new AttendanceGroupUser();
            user.setGroupId(request.getGroupId());
            user.setUserId(Long.valueOf(id));
            user.setUserName(userNames[index]);
            user.setSchoolId(SchoolUtil.getSchoolId());
            user.setUserType(2);
            attendanceGroupUserService.insertAttendanceGroupUser(user);
            index++;
        }
        return AjaxResult.success();
    }

    /**
     * 删除考勤组
     */
    @RequiresPermissions("school:group:remove")
    @Log(title = "考勤组用户", businessType = BusinessType.DELETE)
    @PostMapping( "/removeGroupUer")
    @ResponseBody
    public AjaxResult removeGroupUer(String ids)
    {
        return toAjax(attendanceGroupUserService.deleteAttendanceGroupUserByIds(ids));
    }


}
